/* Includes ------------------------------------------------------------------*/
#include <stdio.h>
#include <string.h>
#include "LCD_SETUP.h"
#include "Icon_Pics.h"
#include "stm32f4xx.h"
#include "stm32f4_discovery.h"
#include "stm32f4_discovery_lcd.h"
#include "stmpe811qtr.h"
#include "LCD_Touch_Calibration.h"


/* Private Variables  */
char isCelsius = 0;
char one;
char two; 
char three; 
char four; 
char isPM;
char oneAlarm, twoAlarm, threeAlarm, fourAlarm, isPMAlarm;
int dayWeek, thisMonth, day, year;
int inside, outside;
int celInside=0,celOutside=0;
char doDraw;
char timePosition;
char playButton = 0;
char notAlarm=0;




void LCD_Setup_Init()
{
  GPIO_InitTypeDef  GPIO_InitStructure;
  GPIO_InitTypeDef  GPIO_InitStructure2;
  GPIO_InitTypeDef  GPIO_InitStructure3;
USART_InitTypeDef USART_InitStructure;
  NVIC_InitTypeDef NVIC_InitStructure; 
   /* Initialize the LCD */
    STM32f4_Discovery_LCD_Init();
    /* Clear the LCD */ 
     LCD_Clear(White); 
     LCD_SetTextColor(Grey);
     IOE_Config();
     /* GPIOD Periph clock enable */
  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);

  /* Configure PE, in output pushpull mode */
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5|GPIO_Pin_4|GPIO_Pin_6;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
  GPIO_Init(GPIOB, &GPIO_InitStructure);
  
  GPIO_InitStructure2.GPIO_Pin = GPIO_Pin_10|GPIO_Pin_13;
  GPIO_InitStructure2.GPIO_Mode = GPIO_Mode_OUT;
  GPIO_InitStructure2.GPIO_OType = GPIO_OType_PP;
  GPIO_InitStructure2.GPIO_Speed = GPIO_Speed_100MHz;
  GPIO_InitStructure2.GPIO_PuPd = GPIO_PuPd_NOPULL;
  GPIO_Init(GPIOA, &GPIO_InitStructure2);
  
  GPIO_InitStructure3.GPIO_Pin = GPIO_Pin_15;
  GPIO_InitStructure3.GPIO_Mode = GPIO_Mode_IN;
  GPIO_InitStructure3.GPIO_Speed = GPIO_Speed_100MHz;
  GPIO_InitStructure3.GPIO_PuPd = GPIO_PuPd_DOWN;
  GPIO_Init(GPIOA, &GPIO_InitStructure3);
  
  
  USART_InitStructure.USART_BaudRate = 9600;
  USART_InitStructure.USART_WordLength = USART_WordLength_8b;
  USART_InitStructure.USART_StopBits = USART_StopBits_1;
  USART_InitStructure.USART_Parity = USART_Parity_No;
  USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  USART_InitStructure.USART_Mode = USART_Mode_Rx|USART_Mode_Tx;
  STM_EVAL_COMInit(COM1, &USART_InitStructure);
  
  USART_ITConfig(EVAL_COM1, USART_IT_RXNE, ENABLE); // enable the USART1 receive interrupt 

	NVIC_InitStructure.NVIC_IRQChannel = USART6_IRQn;		 // we want to configure the USART1 interrupts
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;// this sets the priority group of the USART1 interrupts
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;		 // this sets the subpriority inside the group
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;			 // the USART1 interrupts are globally enabled
	NVIC_Init(&NVIC_InitStructure);							 // the properties are passed to the NVIC_Init function which takes care of the low level stuff	

	// finally this enables the complete USART1 peripheral
	USART_Cmd(EVAL_COM1, ENABLE);
  
 
}
/* Sets up LCD on startup, dummy values are given */
void LCD_ScreenSetup()
{
	 
 
  inside=-99;
  outside=-99;
  
  /* Set the LCD Text size */
  LCD_SetFont(&Font8x12);

   //Lcd_Touch_Calibration();
   setScreenLayout();
   //setInsideTemp(inside); 
   //setOutsideTemp(outside);
   drawTemp();
   setTime('1','2','0','0',0);
   drawTime();
   setDate(0,0,1,2013);
   drawDate();
   setAlarm('0','9','0','0',0);
   drawAlarm();
   isOnSettings(1);
   setTimePosition(0);
	
  
   
}
/*  Sets overall screen layout and labels. Used when screen is cleared */
void setScreenLayout()
{
	char i;

	uint8_t text[50];
	LCD_SetFont(&Font8x12);
	
	/*    Setting up Date Format  */
	sprintf((char*)text,"   DayofWeek, Month, Day, Year");
        LCD_DisplayStringLine(LINE(2),text);
	
	
	/*    Setting up Alarm    */
	LCD_SetFont(&Font8x8);
	sprintf((char*)text,"           Alarm: ");
       LCD_DisplayStringLine(LINE(17),text);
	
	/*     Setting up Temperature    */
	LCD_SetFont(&Font8x12);
	sprintf((char*)text,"  Inside         Outside ");
        LCD_DisplayStringLine(LINE(13),text);
        
	LCD_SetFont(&Font16x24);
	sprintf((char*)text," NA      NA");
        LCD_DisplayStringLine(LINE(18),text);
	LCD_SetFont(&Font8x8);
	LCD_DisplayChar(LINE(22), 46, 0x6F);
	LCD_DisplayChar(LINE(22), 174, 0x6F);
	
        /*   Setting up Media Buttons     */ 
        drawPreviousIcon();
        if(playButton ==0)
        {
          drawPlayIcon();
        }
        else
        {
          drawPauseIcon();
        }
        drawNextIcon();
        drawSettingsIcon();
        drawSunIcon();
        

        //     Draw Volume    ///
        LCD_DrawLine(60, 220 , 195, 0);
	  for(i = 0; i<5;i++)
	  {
		  LCD_DrawLine(40, 218+i , 15, 0);
	  }
          
          for(i = 0; i<5;i++)
	  {
		  LCD_DrawLine(265, 218+i , 15, 0);
	  }
          for(i = 0; i<5;i++)
	  {
		  LCD_DrawLine(270+i, 213, 15, 1);
	  }
          for(i=0;i<3;i++)
	  {
            LCD_DrawLine(60, 218+i , 182, 0);
	  }
	  for(i=0;i<3;i++)
	  {
            LCD_DrawLine(60, 221+i , 182, 0);
	  }
	  
}

/*  Draws alarm on main LCD display   */
void drawAlarm()
{
  if(doDraw==1)
  {
  uint8_t text[50];
	
	LCD_SetFont(&Font8x8);
	LCD_ClearLine(LINE(17));
       
	if(isPMAlarm == 1)
	{
              sprintf((char*)text,"           Alarm: %c%c:%c%c PM",oneAlarm,twoAlarm,threeAlarm,fourAlarm);
              LCD_DisplayStringLine(LINE(17),text);
	}
	else
	{
              sprintf((char*)text,"           Alarm: %c%c:%c%c AM",oneAlarm,twoAlarm,threeAlarm,fourAlarm);
              LCD_DisplayStringLine(LINE(17),text);
	}
  }
}
/*    Function to set alarm variables  */
void setAlarm(char tensHour, char onesHour, char tensMin, char onesMin, char PM)
{
	oneAlarm=tensHour;
        twoAlarm=onesHour;
        threeAlarm=tensMin;
        fourAlarm=onesMin;
        isPMAlarm=PM;
}

void isOnAlarmScreen(char a)
{
  notAlarm=a;
}
/*    Function to draw the temps on screen   */
void drawTemp()
{
  uint8_t text[50];
  double temp=0;
  double temp2=0;
  temp=(double)inside;
  temp2=(double)outside;
  if(doDraw==1&&notAlarm==0)
  {
  if(isCelsius)
  {
    temp = (temp-32)*(0.555);
    temp2 = (temp2-32)*(0.555);
    celInside=(int)temp;
    celOutside=(int)temp2;
    LCD_SetFont(&Font16x24);
      
        if(celInside==-99&&celOutside==-99)
        {
          sprintf((char*)text," NA      NA");
        }
        else if(celInside>10&&celOutside==-99)
        {
          sprintf((char*)text," %d      NA", celInside);
        }
        else if(celInside==-99&&(celOutside>10||celOutside<0))
        {
          sprintf((char*)text," NA      %d",celOutside);
        }
        else if(celInside==-99&&celOutside<-9)
        {
          sprintf((char*)text," NA     %d",celOutside);
        }
        else if((celInside>10)&&(celOutside>10||(celOutside<0&&celOutside>=-9)))
        {
          sprintf((char*)text," %d      %d", celInside, celOutside);
        }
        else if(celOutside<10&&celOutside>0)
        {
          sprintf((char*)text," %d       %d", celInside, celOutside);
        }
        else if(celOutside<-10)
        {
          sprintf((char*)text," %d     %d", celInside, celOutside);
        }
        LCD_DisplayStringLine(LINE(18),text);
	LCD_SetFont(&Font8x8);
	LCD_DisplayChar(LINE(22), 46, 0x6F);
	LCD_DisplayChar(LINE(22), 174, 0x6F);
         if(celOutside<0 && two < 55 && one < 48&&isPM==1) // before 7 PM, cold
        {
          drawSnowIcon();
        }
        else if(celOutside<0 && two > 55 && one < 48 &&isPM==0)// after 7 AM, cold
        {
          drawSnowIcon();
        }
        else if(celOutside>0 && two < 55 && one < 48 && isPM == 1)//before 7 PM, warm
        {
          drawSunIcon();
        }
        else if(celOutside>0 && two > 55 && one < 48 &&isPM==0)//after 7 AM, warm
        {
          drawSunIcon();
        }
        else// dark
        {
          drawMoonIcon();
        }
  }
  else
  {
	LCD_SetFont(&Font16x24);
        if(inside==-99&&outside==-99)
        {
          sprintf((char*)text," NA      NA");
        }
        else if(inside>10&&outside==-99)
        {
          sprintf((char*)text," %d      NA", inside);
        }
        else if(inside==-99&&(outside>10||outside<0))
        {
          sprintf((char*)text," NA      %d",outside);
        }
         else if(inside==-99&&outside<-9)
        {
          sprintf((char*)text," NA     %d",outside);
        }
        else if((inside>10)&&(outside>10||outside<0))
        {
          sprintf((char*)text," %d      %d", inside, outside);
        }
        else if(outside<10&&outside>0)
        {
          sprintf((char*)text," %d       %d", inside, outside);
        }
        
        LCD_DisplayStringLine(LINE(18),text);
	LCD_SetFont(&Font8x8);
	LCD_DisplayChar(LINE(22), 46, 0x6F);
	LCD_DisplayChar(LINE(22), 174, 0x6F);
         if(outside<32 && two < 55 && one < 48&&isPM==1) // before 7 PM, cold
        {
          drawSnowIcon();
        }
        else if(outside<32 && two > 55 && one < 48 &&isPM==0)// after 7 AM, cold
        {
          drawSnowIcon();
        }
        else if(outside>32 && two < 55 && one < 48 && isPM == 1)//before 7 PM, warm
        {
          drawSunIcon();
        }
        else if(outside>32 && two > 55 && one < 48 &&isPM==0)//after 7 AM, warm
        {
          drawSunIcon();
        }
        else// dark
        {
          drawMoonIcon();
        }
  }
  }
}
/*  Function to set F or C for temp from settings  */
void setTempUnits(char selectUnit)
{
    isCelsius = selectUnit;
}
/*      Function to set temp in correct formatt   */
void setInsideTemp(int firstTemp)
{
  inside=firstTemp;
}
void setOutsideTemp(int secondTemp)
{
  outside=secondTemp;
}
/*     Sets time variables for main clock   */
void setTime(char hour, char hour2, char min, char min2, char isAfter)
{
    one=hour;
    two=hour2;
    three=min;
    four=min2;
    isPM=isAfter;
  
}
/*    Draws main clock on LCD   */
void drawTime()
{
  if(doDraw==1)
  {
	
	char i=0;
	uint8_t text[50];
	int temp=0;
        int position[4];
	int dotPosition[2];
	char timeNum[4];
	
        if(!timePosition)
        {
          dotPosition[0] = 122;
	  dotPosition[1] = 62;
          position[0]=50;
          position[1]=110;
          position[2]=180;
          position[3]=240;
        }
        else
        {
          dotPosition[0] = 142;
	  dotPosition[1] = 62;
          position[0]=70;
          position[1]=130;
          position[2]=200;
          position[3]=260;
        }
	timeNum[0] = one;
	timeNum[1] = two;
	timeNum[2] = three;
	timeNum[3] = four;
  
        LCD_SetFont(&Font12x12);
        
        LCD_ClearLine(LINE(4));
        LCD_ClearLine(LINE(5));
        LCD_ClearLine(LINE(6));
        LCD_ClearLine(LINE(7));
        LCD_ClearLine(LINE(8));
        LCD_ClearLine(LINE(9));
        LCD_ClearLine(LINE(10));
	if(isPM == 1)
	{
		LCD_SetFont(&Font16x24);
		/*    Setting up Date Format  */
                if(!timePosition)
                {       
                    sprintf((char*)text,"                PM");
		}
                else
                {
                    sprintf((char*)text,"                 PM");
                }
                LCD_DisplayStringLine(LINE(4),text);
	}
	else
	{
		LCD_SetFont(&Font16x24);
		/*    Setting up Date Format  */
		if(!timePosition)
                {       
                    sprintf((char*)text,"                AM");
		}
                else
                {
                    sprintf((char*)text,"                 AM");
                }
		LCD_DisplayStringLine(LINE(4),text);
	}
	LCD_SetFont(&Font8x12);
	// draw the numbers  //
	for(i=0; i<4; i++)
	{
		switch(timeNum[i])
		{
                        case '0':
                          drawZero(position[i]);
                          break;
                        case '1':
                          drawOne(position[i]);
                          break;
                        case '2' :
                          drawTwo(position[i]);
                          break;
			case '3' :
                          drawThree(position[i]);
                          break;
			case '4' :
                          drawFour(position[i]);
                          break;
                        case '5' :
                          drawFive(position[i]);
                          break;
			case '6' :
                          drawSix(position[i]);
                          break;
			case '7' :
                          drawSeven(position[i]);
                          break;
			case '8' :
                          drawEight(position[i]);
                          break;
			case '9' :
                          drawNine(position[i]);
                          break;
			default:
                          if(!timePosition)
                          {       
                            position[1]=50;
                            position[2]=120;
                            position[3]=180;
                            temp=1;
                          }
                          else
                          {
                            position[1]=130;
                            position[2]=200;
                            position[3]=260;
                            temp=0;
                          }
                        
                          
			  LCD_SetFont(&Font16x24);
			  LCD_ClearLine(LINE(4));
	     
			  if(isPM == 1)
                          {
                                /*    Setting up Date Format  */
				if(!timePosition)
                                {       
                                    sprintf((char*)text,"             PM");
                                }
                                else
                                {
                                    sprintf((char*)text,"                 PM");
                                }
				LCD_DisplayStringLine(LINE(4),text);
                          }
			  else
                          {
                              /*    Setting up Date Format  */
                                if(!timePosition)
                                {       
                                    sprintf((char*)text,"             AM");
                                }
                                else
                                {
                                    sprintf((char*)text,"                 AM");
                                }
				LCD_DisplayStringLine(LINE(4),text);
                          }
                          LCD_SetFont(&Font8x12);
                          
	  }
	
	}
		// draw the dots //
	for(i = 0; i<8;i++)
	{
		LCD_DrawLine(dotPosition[temp]+i, 65 , 8, 1);
	}
	for(i = 0; i<8;i++)
	{
		LCD_DrawLine(dotPosition[temp]+i, 120 , 8, 1);
	}
  }
}

/* Draws date on main LCD display */
void drawDate()
{
  
	uint8_t text[50];
	char *month[12];
	char *dayOfWeek[7];

         dayOfWeek[0]="Sunday";
	  dayOfWeek[1]="Monday";
	  dayOfWeek[2]="Tuesday";
	  dayOfWeek[3]="Wednesday";
	  dayOfWeek[4]="Thursday";
	  dayOfWeek[5]="Friday";
	  dayOfWeek[6]="Saturday";
        if(!timePosition)
        {
	 
          
          month[0] = "January"; 
	  month[1] = "February";
	  month[2] = "March";
	  month[3] = "April";
	  month[4] = "May";
	  month[5] = "June";
	  month[6] = "July";
	  month[7] = "August";
	  month[8] = "September";
	  month[9] = "October";
	  month[10] = "November";
	  month[11] = "December";
        }
        else
        {
          
          
          month[0] = "Jan"; 
	  month[1] = "Feb";
	  month[2] = "Mar";
	  month[3] = "Apr";
	  month[4] = "May";
	  month[5] = "Jun";
	  month[6] = "Jul";
	  month[7] = "Aug";
	  month[8] = "Sep";
	  month[9] = "Oct";
	  month[10] = "Nov";
	  month[11] = "Dec";
        }
        
	
        if(!timePosition)
        {
          LCD_SetFont(&Font8x12);
          LCD_ClearLine(LINE(2));
          sprintf((char*)text,"  %s, %s, %d, %d     ",dayOfWeek[dayWeek],month[thisMonth],day,year);
          LCD_DisplayStringLine(LINE(2),text);
          drawSettingsIcon();
        }
        else
        {
          LCD_SetFont(&Font12x12);
          LCD_ClearLine(LINE(16));
          sprintf((char*)text,"                     %d",year);
          LCD_DisplayStringLine(LINE(16),text);
          sprintf((char*)text,"                 %d",day);
          LCD_DisplayStringLine(LINE(16),text);
          sprintf((char*)text,"            %s",month[thisMonth]);
          LCD_DisplayStringLine(LINE(16),text);
          sprintf((char*)text,"  %s",dayOfWeek[dayWeek]);
          LCD_DisplayStringLine(LINE(16),text);
        }
	    
  
}
/*       Function to set the date variables     */
void setDate(int dayOfWeek, int Month, int thisDay, int thisYear)
{
   dayWeek=dayOfWeek;
   thisMonth=Month;
   day=thisDay;
   year=thisYear;
}

/*   Sets checker for settings screens  */
void isOnSettings(char isSettings)
{
    doDraw=isSettings;
}
/*  Sets checker for time position either in settings or main screen  */
void setTimePosition(char pos)
{
  timePosition = pos;
}

char isAlarm()
{
  if(oneAlarm==one&&twoAlarm==two&&threeAlarm==three&&fourAlarm==four&&isPMAlarm==isPM)
  {
    return 1;
  }
  else if(oneAlarm=='0'&&one=='-'&&twoAlarm==two&&threeAlarm==three&&fourAlarm==four&&isPMAlarm==isPM)
  {
    return 1;
  }
  else
  {
    return 0;
  }
}
/*  Increments the time by minute, changes date and hour if necessary  */
void incrementTime()
{
  incrementMin();
  if(three=='0'&&four=='0')
  {
    incrementHour();
    if(one!='1'&&two=='1')
    {
      changeHalfDay();
      if(isPM=='0')
      {
        incrementDayofWeek();
        incrementDayofMonth();
        if(day==1)
        {
          incrementMonth();
          if(thisMonth==1)
          {
            incrementYear();
          }
        }
        drawDate();
      } 
    }
  }

}
void incrementHour()
{
  if((one<'0'&&two<'9')||(one=='1'&&two<'2'))
  {
   two++;
  }
  else if(one<'0'&&two=='9')
  {
    one='1';
    two='0';
  }
  else if(one=='1'&&two=='2')
  {
    one='-';
    two='1';
  }
  drawTime();
   
}
void decrementHour()
{
  if((one<'0'&&two>'1')||(one=='1'&&two<='2'&&two!='0'))
  {
    two--;
  }
  else if(one<'0'&&two=='1')
  {
    one='1';
    two='2';
  }
  else if(one=='1'&&two=='0')
  {
    one='-';
    two='9';
  }
  drawTime();
}
void incrementMin()
{
  if(four<'9')
  {
    four++;
  }
  else if(four=='9'&&three<'5')
  {
    four='0';
    three++;
  }
  else if(four=='9'&&three=='5')
  {
    four='0';
    three='0';
  }
  drawTime();
  
}
void decrementMin()
{
  if(four>'0')
  {
    four--;
  }
  else if(four=='0'&&three>'0')
  {
    four='9';
    three--;
  }
  else if(four=='0'&&three=='0')
  {
    four='9';
    three='5';
  }
  drawTime();
}
void changeHalfDay()
{
  if(isPM)
  {
    isPM=0;
  }
  else
  {
    isPM=1;
  }
  drawTime();
}

void mediaButton(char isPlay)
{
  playButton=isPlay;
}
void incrementDayofWeek()
{
  if(dayWeek==6)
  {
    dayWeek=0;
  }
  else
  {
    dayWeek++;
  }
    drawDate();
}
void decrementDayofWeek()
{
  if(dayWeek==0)
  {
    dayWeek=6;
  }
  else
  {
    dayWeek--;
  }
  drawDate();
}
void incrementMonth()
{
  if(thisMonth==11)
  {
    thisMonth=0;
  }
  else
  {
    thisMonth++;
  }
  if((thisMonth==3||thisMonth==5||thisMonth==8||thisMonth==10)&&(day==31))
  {
    day=30;
  }
  if(thisMonth==1&&day>28)
  {
    day=28;
  }
  drawDate();
}
void decrementMonth()
{
  if(thisMonth==0)
  {
    thisMonth=11;
  }
  else
  {
    thisMonth--;
  }
  if((thisMonth==3||thisMonth==5||thisMonth==8||thisMonth==10)&&(day==31))
  {
    day=30;
  }
  if(thisMonth==1&&day>28)
  {
    day=28;
  }
  drawDate();
}
void incrementDayofMonth()
{
  if((thisMonth==3||thisMonth==5||thisMonth==8||thisMonth==10)&&(day==30))
  {
    day=1;
  }
  else if((thisMonth==3||thisMonth==5||thisMonth==8||thisMonth==10)&&(day<30))
  {
    day++;
  }
  
  
  if(thisMonth==1&&day==28)
  {
    day=1;
  }
  else if(thisMonth==1&&day<28)
  {
    day++;
  }
  
  
  if((thisMonth==0||thisMonth==2||thisMonth==4||thisMonth==6||thisMonth==7||thisMonth==9||thisMonth==11)&&(day==31))
  {
    day=1;
  }
  else if((thisMonth==0||thisMonth==2||thisMonth==4||thisMonth==6||thisMonth==7||thisMonth==9||thisMonth==11)&&(day<31))
  {
    day++;
  }
  drawDate();
}
void decrementDayofMonth()
{
  if((thisMonth==3||thisMonth==5||thisMonth==8||thisMonth==10)&&(day==1))
  {
    day=30;
  }
  else if((thisMonth==3||thisMonth==5||thisMonth==8||thisMonth==10)&&(day>1))
  {
    day--;
  }
  if(thisMonth==1&&day==1)
  {
    day=28;
  }
  else if(thisMonth==1&&day>1)
  {
    day--;
  }
  if((thisMonth==0||thisMonth==2||thisMonth==4||thisMonth==6||thisMonth==7||thisMonth==9||thisMonth==11)&&(day==1))
  {
    day=31;
  }
  else if((thisMonth==0||thisMonth==2||thisMonth==4||thisMonth==6||thisMonth==7||thisMonth==9||thisMonth==11)&&(day>1))
  {
    day--;
  }
  drawDate();
}
void incrementYear()
{
  year++;
  drawDate();
}
void decrementYear()
{
  if(year>2013)
  {
    year--;
    drawDate();
  }
}


/* Function to parse data received from Bluetooth */
void configBluetooth(char *s)
{
  int ten, ones;
  int temp;
  int dayOfMonth;   
  
    // Temperature Set for outside //
    if(s[1]=='-')
    {
      ten = s[2] - '0';
      ten=ten*10;
      ones = s[3] - '0';
      ten+=ones;
      ten=0-ten;
    }
    else
    {
      ten = s[2] - '0';
      ten=ten*10;
      ones = s[3] - '0';
      ten+=ones;
    }
      // Day of Week Set //
     dayWeek=(s[5] - '0')-1;
    
     //    Month Set  //
     thisMonth=s[7]-'0';
     thisMonth=thisMonth*10;
     temp=s[8]-'0';
     thisMonth+=temp;
     
     // Day of Month set //
     day=s[10] - '0';
     day=day*10;
     dayOfMonth=s[11] - '0';
     day+=dayOfMonth;
     
     //  Year set //
     temp=s[13]-'0';
     temp+=2010;
     year=temp;
     
     //  Hours for main Time //
      one = s[15];
      two = s[16];
     
      if(one=='0'&&two=='0')
      {
        isPM=0;
        one='1';
        two='2';
      }
      else if(one=='0')
      {
        one='-';
        isPM=0;
      }
      else if(one=='1'&&two>'2')
      {
        isPM=1;
        one='-';
        two-=2;
      }
      else if(one=='2'&&two=='0')
      {
         one='-';
         two='8';
         isPM=1;
      }
      else if(one=='2'&&two=='1')
      {
        one='-';
        two='9';
        isPM=1;
      }
      else if(one=='2'&&two=='2')
      {
        one='1';
        two='0';
        isPM=1;
      }
      else if(one=='2'&&two=='3')
      {
        one='1';
        two='1';
        isPM=1;
      }
      // Minutes for main time  //
      three = s[18];
      four = s[19];
      
      // Hours for alarm //
     oneAlarm = s[21];
     if(oneAlarm != '9')
     {
        twoAlarm = s[22];
      
     
        if(oneAlarm=='0'&&twoAlarm=='0')
        {
          isPMAlarm=0;
          oneAlarm='1';
          twoAlarm='2';
        }
        else if(oneAlarm=='0')
        {
          oneAlarm='0';
          isPMAlarm=0;
        }
        else if(oneAlarm=='1'&&twoAlarm>'2')
        {
          isPMAlarm=1;
          oneAlarm='0';
          twoAlarm-=2;
        }
        else if(oneAlarm=='2'&&twoAlarm=='0')
        {
          oneAlarm='0';
          twoAlarm='8';
          isPMAlarm=1;
        }
        else if(oneAlarm=='2'&&twoAlarm=='1')
        {
          oneAlarm='0';
          twoAlarm='9';
          isPMAlarm=1;
        }
        else if(oneAlarm=='2'&&twoAlarm=='2')
        {
          oneAlarm='1';
          twoAlarm='0';
          isPMAlarm=1;
        }
        else if(oneAlarm=='2'&&twoAlarm=='3')
        {
          oneAlarm='1';
          twoAlarm='1';
          isPMAlarm=1;
        }
      
        // Minutes for alarm //
        threeAlarm = s[23];    
        fourAlarm = s[24];
        drawAlarm();
     }
       //Draw all Info //
     setOutsideTemp(ten);
     drawTemp();
     drawDate();
     drawTime();
     
  //}
}

// Draw number one for clock  //
void drawOne(int position)
{
	int i = 0;
	int count =1;
	int temp = 0;
	//   Vertical right top //
	for(i=0; i < 7; i++)
	{
		if(i<=2)
		{
			LCD_DrawLine(position-i, 50+i, 35, 1);
			
		}
		else
		{
			LCD_DrawLine(position-i, 50+i, 35-(count*2), 1);
			count++;
		}	
  }
	
	 count = 1;
	// Vertical bottom right //
	for(i=0; i < 7; i++)
	{
		if(i<=2)
		{
			LCD_DrawLine(position-i, 90-i, 35, 1);
			temp = 90-i;
			temp = temp-2;
		}
		else
		{	
			LCD_DrawLine(position-i, temp+i, 35-(count*2), 1);
			count++;
		}	
  }
	
}
// Draw number two for clock  //
void drawTwo(int position)
{
	int i =0;
	int count = 1;
	int temp = 0;
	//   Vertical right top //
	for(i=0; i < 7; i++)
	{
		if(i<=2)
		{
			LCD_DrawLine(position-i, 50+i, 35, 1);
			
		}
		else
		{
			LCD_DrawLine(position-i, 50+i, 35-(count*2), 1);
			count++;
		}	
  }
	//  Horizontal top  //
	
	for(i=0; i < 7; i++)
	{
			LCD_DrawLine((position-36)+i, 48+i, 35-(i*2), 0);
  }
	count = 1;
	//  Horizontal middle   //
	for(i=0; i < 7; i++)
	{
		if(i>=4)
		{
			LCD_DrawLine((position-36)+i, 84+i, temp-(count*2), 0);
			count++;
		}
		else
		{
			LCD_DrawLine((position-30)-i, 84+i, 23+(i*2), 0);
			temp= 23+(i*2);
		}	
  }
	temp=0;
	count =1;
	// Veritcal left bottom //
	for(i=0; i < 7; i++)
	{
		if(i<=2)
		{
			LCD_DrawLine((position-38)+i, 90-i, 35, 1);
			temp = 90-i;
			temp = temp-2;
			
		}
		else
		{
				
			LCD_DrawLine((position-38)+i, temp+i, 35-(count*2), 1);
			count++;
		}	
  }
	//  Horizontal bottom   //
	for(i=0; i < 7; i++)
	{
			LCD_DrawLine((position-36)+i, 127-i, 35-(i*2), 0);
  }
	
}
// Draw number three for clock  //
void drawThree(int position)
{
	int i =0;
	int count = 1;
	int temp = 0;
	//   Vertical right top //
	for(i=0; i < 7; i++)
	{
		if(i<=2)
		{
			LCD_DrawLine(position-i, 50+i, 35, 1);
			
		}
		else
		{
			LCD_DrawLine(position-i, 50+i, 35-(count*2), 1);
			count++;
		}	
  }
	//  Horizontal top  //
	
	for(i=0; i < 7; i++)
	{
			LCD_DrawLine((position-36)+i, 48+i, 35-(i*2), 0);
  }
	count = 1;
	//  Horizontal middle   //
	for(i=0; i < 7; i++)
	{
		if(i>=4)
		{
			LCD_DrawLine((position-36)+i, 84+i, temp-(count*2), 0);
			count++;
		}
		else
		{
			LCD_DrawLine((position-30)-i, 84+i, 23+(i*2), 0);
			temp= 23+(i*2);
		}	
  }
	temp=0;
	count =1;
	// Vertical bottom right //
	for(i=0; i < 7; i++)
	{
		if(i<=2)
		{
			LCD_DrawLine(position-i, 90-i, 35, 1);
			temp = 90-i;
			temp = temp-2;
		}
		else
		{	
			LCD_DrawLine(position-i, temp+i, 35-(count*2), 1);
			count++;
		}	
  }
	//  Horizontal bottom   //
	for(i=0; i < 7; i++)
	{
			LCD_DrawLine((position-36)+i, 127-i, 35-(i*2), 0);
  }
}
// Draw number four for clock  //
void drawFour(int position)
{
	int i =0;
	int count = 1;
	int temp = 0;
	//   Vertical right top //
	for(i=0; i < 7; i++)
	{
		if(i<=2)
		{
			LCD_DrawLine(position-i, 50+i, 35, 1);
			
		}
		else
		{
			LCD_DrawLine(position-i, 50+i, 35-(count*2), 1);
			count++;
		}	
  }
	temp=0;
	count=1;
	//  Horizontal middle   //
	for(i=0; i < 7; i++)
	{
		if(i>=4)
		{
			LCD_DrawLine((position-36)+i, 84+i, temp-(count*2), 0);
			count++;
		}
		else
		{
			LCD_DrawLine((position-30)-i, 84+i, 23+(i*2), 0);
			temp= 23+(i*2);
		}	
  }
	temp=0;
	count =1;
	// Vertical bottom right //
	for(i=0; i < 7; i++)
	{
		if(i<=2)
		{
			LCD_DrawLine(position-i, 90-i, 35, 1);
			temp = 90-i;
			temp = temp-2;
		}
		else
		{	
			LCD_DrawLine(position-i, temp+i, 35-(count*2), 1);
			count++;
		}	
  }
	count = 1;
		//   vertical left top  //
	for(i=0; i < 7; i++)
	{
		if(i<=2)
		{
			LCD_DrawLine((position-38)+i, 50+i, 35, 1);
			
		}
		else
		{
				
			LCD_DrawLine((position-38)+i, 50+i, 35-(count*2), 1);
			count++;
		}	
  }
	
}

//  Draw number five for clock  //
void drawFive(int position)
{
	int i =0;
	int count = 1;
	int temp = 0;
	
	
	//  Horizontal top  //
	
	for(i=0; i < 7; i++)
	{
			LCD_DrawLine((position-36)+i, 48+i, 35-(i*2), 0);
  }
	count = 1;
	temp=0;
	count=1;
	//  Horizontal middle   //
	for(i=0; i < 7; i++)
	{
		if(i>=4)
		{
			LCD_DrawLine((position-36)+i, 84+i, temp-(count*2), 0);
			count++;
		}
		else
		{
			LCD_DrawLine((position-30)-i, 84+i, 23+(i*2), 0);
			temp= 23+(i*2);
		}	
  }
	//  Horizontal bottom   //
	for(i=0; i < 7; i++)
	{
			LCD_DrawLine((position-36)+i, 127-i, 35-(i*2), 0);
  }
	temp=0;
	count =1;
	// Vertical bottom right //
	for(i=0; i < 7; i++)
	{
		if(i<=2)
		{
			LCD_DrawLine(position-i, 90-i, 35, 1);
			temp = 90-i;
			temp = temp-2;
		}
		else
		{	
			LCD_DrawLine(position-i, temp+i, 35-(count*2), 1);
			count++;
		}	
  }
	count = 1;
		//   vertical left top  //
	for(i=0; i < 7; i++)
	{
		if(i<=2)
		{
			LCD_DrawLine((position-38)+i, 50+i, 35, 1);
			
		}
		else
		{
				
			LCD_DrawLine((position-38)+i, 50+i, 35-(count*2), 1);
			count++;
		}	
  }
}

// Draw number six for clock //
void drawSix(int position)
{
	int i =0;
	int count = 1;
	int temp = 0;
	
	
	//  Horizontal top  //
	
	for(i=0; i < 7; i++)
	{
			LCD_DrawLine((position-36)+i, 48+i, 35-(i*2), 0);
  }
	count = 1;
	temp=0;
	count=1;
	//  Horizontal middle   //
	for(i=0; i < 7; i++)
	{
		if(i>=4)
		{
			LCD_DrawLine((position-36)+i, 84+i, temp-(count*2), 0);
			count++;
		}
		else
		{
			LCD_DrawLine((position-30)-i, 84+i, 23+(i*2), 0);
			temp= 23+(i*2);
		}	
  }
	//  Horizontal bottom   //
	for(i=0; i < 7; i++)
	{
			LCD_DrawLine((position-36)+i, 127-i, 35-(i*2), 0);
  }
	temp=0;
	count =1;
	// Vertical bottom right //
	for(i=0; i < 7; i++)
	{
		if(i<=2)
		{
			LCD_DrawLine(position-i, 90-i, 35, 1);
			temp = 90-i;
			temp = temp-2;
		}
		else
		{	
			LCD_DrawLine(position-i, temp+i, 35-(count*2), 1);
			count++;
		}	
  }
	count = 1;
		//   vertical left top  //
	for(i=0; i < 7; i++)
	{
		if(i<=2)
		{
			LCD_DrawLine((position-38)+i, 50+i, 35, 1);
			
		}
		else
		{
				
			LCD_DrawLine((position-38)+i, 50+i, 35-(count*2), 1);
			count++;
		}	
  }
	temp=0;
	count =1;
	// Veritcal left bottom //
	for(i=0; i < 7; i++)
	{
		if(i<=2)
		{
			LCD_DrawLine((position-38)+i, 90-i, 35, 1);
			temp = 90-i;
			temp = temp-2;
			
		}
		else
		{
				
			LCD_DrawLine((position-38)+i, temp+i, 35-(count*2), 1);
			count++;
		}	
  }
}

// Draw number seven for clock //
void drawSeven(int position)
{
	int i =0;
	int count = 1;
	int temp = 0;
	
	
	//  Horizontal top  //
	
	for(i=0; i < 7; i++)
	{
			LCD_DrawLine((position-36)+i, 48+i, 35-(i*2), 0);
  }
	//   Vertical right top //
	for(i=0; i < 7; i++)
	{
		if(i<=2)
		{
			LCD_DrawLine(position-i, 50+i, 35, 1);
			
		}
		else
		{
			LCD_DrawLine(position-i, 50+i, 35-(count*2), 1);
			count++;
		}
	}
	temp=0;
	count =1;
	// Vertical bottom right //
	for(i=0; i < 7; i++)
	{
		if(i<=2)
		{
			LCD_DrawLine(position-i, 90-i, 35, 1);
			temp = 90-i;
			temp = temp-2;
		}
		else
		{	
			LCD_DrawLine(position-i, temp+i, 35-(count*2), 1);
			count++;
		}	
  }
}
// Draw number eight for clock  //
void drawEight(int position)
{
	int i =0;
	int count = 1;
	int temp = 0;
	
	
	//  Horizontal top  //
	
	for(i=0; i < 7; i++)
	{
			LCD_DrawLine((position-36)+i, 48+i, 35-(i*2), 0);
  }
	count = 1;
	temp=0;
	count=1;
	//  Horizontal middle   //
	for(i=0; i < 7; i++)
	{
		if(i>=4)
		{
			LCD_DrawLine((position-36)+i, 84+i, temp-(count*2), 0);
			count++;
		}
		else
		{
			LCD_DrawLine((position-30)-i, 84+i, 23+(i*2), 0);
			temp= 23+(i*2);
		}	
  }
	//  Horizontal bottom   //
	for(i=0; i < 7; i++)
	{
			LCD_DrawLine((position-36)+i, 127-i, 35-(i*2), 0);
  }
	count =1;
	//   Vertical right top //
	for(i=0; i < 7; i++)
	{
		if(i<=2)
		{
			LCD_DrawLine(position-i, 50+i, 35, 1);
			
		}
		else
		{
			LCD_DrawLine(position-i, 50+i, 35-(count*2), 1);
			count++;
		}
	}
	temp=0;
	count =1;
	// Vertical bottom right //
	for(i=0; i < 7; i++)
	{
		if(i<=2)
		{
			LCD_DrawLine(position-i, 90-i, 35, 1);
			temp = 90-i;
			temp = temp-2;
		}
		else
		{	
			LCD_DrawLine(position-i, temp+i, 35-(count*2), 1);
			count++;
		}	
  }
	count = 1;
		//   vertical left top  //
	for(i=0; i < 7; i++)
	{
		if(i<=2)
		{
			LCD_DrawLine((position-38)+i, 50+i, 35, 1);
			
		}
		else
		{
				
			LCD_DrawLine((position-38)+i, 50+i, 35-(count*2), 1);
			count++;
		}	
  }
	temp=0;
	count =1;
	// Veritcal left bottom //
	for(i=0; i < 7; i++)
	{
		if(i<=2)
		{
			LCD_DrawLine((position-38)+i, 90-i, 35, 1);
			temp = 90-i;
			temp = temp-2;
			
		}
		else
		{
				
			LCD_DrawLine((position-38)+i, temp+i, 35-(count*2), 1);
			count++;
		}	
  }
	
}
// Draw number nine for clock //
void drawNine(int position)
{
	int i =0;
	int count = 1;
	int temp = 0;
	
	
	//  Horizontal top  //
	
	for(i=0; i < 7; i++)
	{
			LCD_DrawLine((position-36)+i, 48+i, 35-(i*2), 0);
  }
	count = 1;
	temp=0;
	count=1;
	//  Horizontal middle   //
	for(i=0; i < 7; i++)
	{
		if(i>=4)
		{
			LCD_DrawLine((position-36)+i, 84+i, temp-(count*2), 0);
			count++;
		}
		else
		{
			LCD_DrawLine((position-30)-i, 84+i, 23+(i*2), 0);
			temp= 23+(i*2);
		}	
  }
	//  Horizontal bottom   //
	for(i=0; i < 7; i++)
	{
			LCD_DrawLine((position-36)+i, 127-i, 35-(i*2), 0);
  }
	count =1;
	//   Vertical right top //
	for(i=0; i < 7; i++)
	{
		if(i<=2)
		{
			LCD_DrawLine(position-i, 50+i, 35, 1);
			
		}
		else
		{
			LCD_DrawLine(position-i, 50+i, 35-(count*2), 1);
			count++;
		}
	}
	temp=0;
	count =1;
	// Vertical bottom right //
	for(i=0; i < 7; i++)
	{
		if(i<=2)
		{
			LCD_DrawLine(position-i, 90-i, 35, 1);
			temp = 90-i;
			temp = temp-2;
		}
		else
		{	
			LCD_DrawLine(position-i, temp+i, 35-(count*2), 1);
			count++;
		}	
  }
	count = 1;
		//   vertical left top  //
	for(i=0; i < 7; i++)
	{
		if(i<=2)
		{
			LCD_DrawLine((position-38)+i, 50+i, 35, 1);
		}
		else
		{
			LCD_DrawLine((position-38)+i, 50+i, 35-(count*2), 1);
			count++;
		}	
  }
}
// Draw number zero for clock //
void drawZero(int position)
{
	int i =0;
	int count = 1;
	int temp = 0;
	
	
	//  Horizontal top  //
	
	for(i=0; i < 7; i++)
	{
			LCD_DrawLine((position-36)+i, 48+i, 35-(i*2), 0);
  }
	
	//  Horizontal bottom   //
	for(i=0; i < 7; i++)
	{
			LCD_DrawLine((position-36)+i, 127-i, 35-(i*2), 0);
  }
	count =1;
	//   Vertical right top //
	for(i=0; i < 7; i++)
	{
		if(i<=2)
		{
			LCD_DrawLine(position-i, 50+i, 35, 1);
			
		}
		else
		{
			LCD_DrawLine(position-i, 50+i, 35-(count*2), 1);
			count++;
		}
	}
	temp=0;
	count =1;
	// Vertical bottom right //
	for(i=0; i < 7; i++)
	{
		if(i<=2)
		{
			LCD_DrawLine(position-i, 90-i, 35, 1);
			temp = 90-i;
			temp = temp-2;
		}
		else
		{	
			LCD_DrawLine(position-i, temp+i, 35-(count*2), 1);
			count++;
		}	
  }
	count = 1;
		//   vertical left top  //
	for(i=0; i < 7; i++)
	{
		if(i<=2)
		{
			LCD_DrawLine((position-38)+i, 50+i, 35, 1);
			
		}
		else
		{
				
			LCD_DrawLine((position-38)+i, 50+i, 35-(count*2), 1);
			count++;
		}	
  }
	temp=0;
	count =1;
	// Veritcal left bottom //
	for(i=0; i < 7; i++)
	{
		if(i<=2)
		{
			LCD_DrawLine((position-38)+i, 90-i, 35, 1);
			temp = 90-i;
			temp = temp-2;
			
		}
		else
		{
				
			LCD_DrawLine((position-38)+i, temp+i, 35-(count*2), 1);
			count++;
		}	
  }
	

}

